package me.zhanshi123.lab4.controller;

import me.zhanshi123.lab4.entity.ApiResponse;
import me.zhanshi123.lab4.entity.AuthIdentity;
import me.zhanshi123.lab4.service.IUserService;
import me.zhanshi123.lab4.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/changePassword")
    public String changePassword(
            @RequestParam("password") String newPwd,
            @RequestParam("name") String name,
            HttpServletRequest request) {
        AuthIdentity authIdentity = (AuthIdentity) request.getAttribute("auth");
        return userService.changePassword(authIdentity, name, newPwd);
    }

    @RequestMapping("/updateSelf")
    public String updateSelf(@RequestParam("email") String email,
                             HttpServletRequest request) {
        AuthIdentity authIdentity = (AuthIdentity) request.getAttribute("auth");
        return userService.updateSelf(authIdentity, email);
    }

    @RequestMapping("/info")
    public String getInfo(HttpServletRequest request) {
        AuthIdentity authIdentity = (AuthIdentity) request.getAttribute("auth");
        return userService.getInfo(authIdentity);
    }

    @RequestMapping("/all")
    public String getAll() {
        return userService.getAll();
    }

    @RequestMapping("/updateOther")
    public String updateOther(@RequestParam("user") String user,
                              HttpServletRequest request) {
        AuthIdentity authIdentity = (AuthIdentity) request.getAttribute("auth");
        return userService.updateOther(authIdentity, user);
    }

    @RequestMapping("/add")
    public String updateOther(@RequestParam("password") String password,
                              @RequestParam("name") String name,
                              @RequestParam("email") String email,
                              HttpServletRequest request) {
        AuthIdentity authIdentity = (AuthIdentity) request.getAttribute("auth");
        return userService.add(authIdentity, name, password, email);
    }
}
